package com.smarth.zhizulian.http.utils;

import com.smarth.zhizulian.bean.BaseEntity;
import com.smarth.zhizulian.http.exception.ApiException;
import com.smarth.zhizulian.http.exception.NoNetException;
import com.smarth.zhizulian.http.exception.UnLoginException;
import com.smarth.zhizulian.utils.UserUtils;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by KUMA on 2017/10/8.
 */

public class RxCompos {
    /**
     * 获取http线程compos
     *
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<T, T> getHttpCompos() {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(@NonNull Observable<T> upstream) {
                return buildHttpObservable(upstream);
            }
        };
    }

    //http线程
    private static <T> Observable buildHttpObservable(final Observable<T> observable) {
        return observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        if (!NetConnectUtils.isNetworkConnected()) {
//                            disposable.dispose();
                            throw new NoNetException();
                        }
                    }
                });
    }

    //http线程
    private static <T> Observable buildHttpLoginObservable(final Observable<T> observable) {
        return observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        if (!NetConnectUtils.isNetworkConnected()) {
//                            disposable.dispose();
                            throw new NoNetException();
                        }
                        if (!UserUtils.isLogin()) {
//                            disposable.dispose();
                            throw new UnLoginException();
                        }
                    }
                });
    }

    /**
     * 获取http线程，转换BaseEntity，compos
     *
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<BaseEntity<T>, T> getBECompos() {
        return new ObservableTransformer<BaseEntity<T>, T>() {
            @Override
            public ObservableSource<T> apply(@NonNull Observable<BaseEntity<T>> upstream) {
                return buildHttpObservable(upstream)
                        .map(new Function<BaseEntity<T>, T>() {
                            @Override
                            public T apply(BaseEntity<T> tBaseEntity) throws Exception {
                                if (!tBaseEntity.isSuccess()) {
                                    throw new ApiException(tBaseEntity);
                                }
                                return tBaseEntity.getData();
                            }
                        });
            }
        };
    }

    /**
     * 获取http线程，转换BaseEntity， 登录判断，compos
     *
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<BaseEntity<T>, T> getBELoginCompos() {
        return new ObservableTransformer<BaseEntity<T>, T>() {
            @Override
            public ObservableSource<T> apply(@NonNull Observable<BaseEntity<T>> upstream) {
                return buildHttpLoginObservable(upstream)
                        .map(new Function<BaseEntity<T>, T>() {
                            @Override
                            public T apply(BaseEntity<T> tBaseEntity) throws Exception {
                                if (!tBaseEntity.isSuccess()) {
                                    throw new ApiException(tBaseEntity);
                                }
                                return tBaseEntity.getData();
                            }
                        });
            }
        };
    }

    /**
     * 获取http线程，转换BaseEntity， 登录判断，compos
     *
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<BaseEntity<T>, BaseEntity<T>> getBEAllLoginCompos() {
        return new ObservableTransformer<BaseEntity<T>, BaseEntity<T>>() {
            @Override
            public ObservableSource<BaseEntity<T>> apply(@NonNull Observable<BaseEntity<T>> upstream) {
                return buildHttpLoginObservable(upstream)
                        .map(new Function<BaseEntity<T>, BaseEntity<T>>() {
                            @Override
                            public BaseEntity<T> apply(BaseEntity<T> tBaseEntity) throws Exception {
                                if (!tBaseEntity.isSuccess()) {
                                    throw new ApiException(tBaseEntity);
                                }
                                return tBaseEntity;
                            }
                        });
            }
        };
    }

    /**
     * 获取http线程，转换BaseEntity， 登录判断，compos
     *
     * @return
     */
    public static ObservableTransformer<BaseEntity, BaseEntity> getBEAll2LoginCompos() {
        return new ObservableTransformer<BaseEntity, BaseEntity>() {
            @Override
            public ObservableSource<BaseEntity> apply(@NonNull Observable<BaseEntity> upstream) {
                return buildHttpLoginObservable(upstream)
                        .map(new Function<BaseEntity, BaseEntity>() {
                            @Override
                            public BaseEntity apply(BaseEntity tBaseEntity) throws Exception {
                                if (!tBaseEntity.isSuccess()) {
                                    throw new ApiException(tBaseEntity);
                                }
                                return tBaseEntity;
                            }
                        });
            }
        };
    }

    /**
     * 获取http线程，转换BaseEntity，compos
     *
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<BaseEntity<T>, BaseEntity<T>> getBEAllCompos() {
        return new ObservableTransformer<BaseEntity<T>, BaseEntity<T>>() {
            @Override
            public ObservableSource<BaseEntity<T>> apply(@NonNull Observable<BaseEntity<T>> upstream) {
                return buildHttpObservable(upstream)
                        .map(new Function<BaseEntity<T>, BaseEntity<T>>() {
                            @Override
                            public BaseEntity<T> apply(BaseEntity<T> tBaseEntity) throws Exception {
                                if (!tBaseEntity.isSuccess()) {
                                    throw new ApiException(tBaseEntity);
                                }
                                return tBaseEntity;
                            }
                        });
            }
        };
    }

    /**
     * 获取http线程，登录判断，compos
     *
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<T, T> getLoginCompos() {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(@NonNull Observable<T> upstream) {
                return buildHttpLoginObservable(upstream);
            }
        };
    }


}
